{$MODE OBJFPC}
{$M+}
uses
  graph,winmouse;
const
  //OPT_mouse
    ONCLICK=1;
  //PLATE_prop
    MAXX=640;
    MAXY=480;
  //ATO_type
    EMPTY=0;
    DUST=1;
    WOOD=2;
  //ATO_state
    POWDER=1;
    LIQUID=2;
    SOILD=3;
    GAS=4;
type
  Tloc=class
    public
      x,y:longint;
      constructor Tloc(a,b:longint);
  end;
  Tato=class
    private
      flag:boolean;
      num:longint;
      typ,sta:longint;
      x,y:longint;
    public
      constructor Tato();
      function p_isexist():boolean;
      procedure p_new(a,b,c,d,e:longint);
      procedure p_destory();
      procedure s_next();
      function p_weight():longint;
      function g_x():longint;
      function g_y():longint;
      function g_typ():longint;
  end;
  Tpla=class
    private
      gasmap:array[-10..MAXX+10,-10..MAXY+10] of longint;
      atomap:array[-10..MAXX+10,-10..MAXY+10] of longint;
      tempmap:array[-10..MAXX+10,-10..MAXY+10] of longint;
    public
      constructor Tpla();
      procedure putato(a:longint);
      procedure moveato();
      function balancegas(a,b:longint):Tloc;
  end;
var
  //Variable
    gd,gm:smallint;
    mx,my,mf:longint;
    now:longint;
    i,j,tmp:longint;
    plate:Tpla;
    ato:array[0..MAXX*MAXY+10] of Tato;

  //Function
    function ato_findempty():longint; var i:longint; begin i:=0; repeat inc(i); until not ato[i].p_isexist(); exit(i); end;
    function ato_typ2sta(a:longint):longint;
      begin
        if a=EMPTY then exit(0);
        if a=DUST then exit(POWDER);
        if a=WOOD then exit(SOILD);
      end;

  //Solve
    constructor Tloc.Tloc(a,b:longint); begin x:=a; y:=b; end;

    constructor Tato.Tato(); begin flag:=false; typ:=0; end;
    function Tato.p_isexist():boolean; begin exit(flag); end;
    procedure Tato.p_new(a,b,c,d,e:longint); begin flag:=true; num:=a; typ:=b; sta:=c; x:=d; y:=e; end;
    procedure Tato.p_destory(); begin flag:=false; writeln('ATO ',num,' Destoried !'); end;
    procedure Tato.s_next();
      begin
        ato[0].p_new(0,0,1,x,y);
        plate.putato(0);
        putpixel(x,y,0);
        if ato_typ2sta(plate.atomap[x,y+1])=0 then
          begin
            inc(x,random(3)-1);
            inc(y,1);
          end
        else
          begin
            if ato_typ2sta(plate.atomap[x,y+1])<>LIQUID then
              begin
                if ato_typ2sta(plate.atomap[x+1,y])=0 then inc(x);
                if ato_typ2sta(plate.atomap[x-1,y])=0 then dec(x);
              end
            else
              begin
                inc(x,random(3)-1);
              end;
          end;
        if (x>0) and (y>0) and (x<maxx+1) and (y<maxy+1) then
          begin
            plate.putato(num);
            putpixel(x,y,10);
          end
        else
          p_destory();
      end;
    function Tato.p_weight():longint;
      begin
        if typ=EMPTY then exit(0);
        if typ=DUST then exit(1300);
      end;
    function Tato.g_x():longint; begin exit(x); end;
    function Tato.g_y():longint; begin exit(y); end;
    function Tato.g_typ():longint; begin exit(typ); end;

    constructor Tpla.Tpla(); begin fillchar(atomap,sizeof(atomap),0); end;
    procedure Tpla.putato(a:longint); begin atomap[ato[a].g_x(),ato[a].g_y()]:=ato[a].g_typ(); end;
    procedure Tpla.moveato(); var i:longint; begin for i:=1 to MAXX*MAXY do if ato[i].p_isexist() then ato[i].s_next(); end;
    function Tpla.balancegas(a,b:longint):Tloc;
      begin

      end;
begin
  randomize;
  gd:=vga; gm:=vgahi;
  initgraph(gd,gm,'');

  now:=DUST;
  for i:=0 to MAXX*MAXY do ato[i]:=Tato.Tato();
  plate:=Tpla.Tpla();

  for i:=100 to 500 do
  begin
    ato[0].p_new(0,2,1,i,300); plate.putato(0);
    ato[0].p_new(0,2,1,i,301); plate.putato(0);
    ato[0].p_new(0,2,1,i,302); plate.putato(0);
    ato[0].p_new(0,2,1,i,303); plate.putato(0);
    ato[0].p_new(0,2,1,i,304); plate.putato(0);
    ato[0].p_new(0,2,1,i,305); plate.putato(0);
  end;

  repeat
    getmousestate(mx,my,mf);
    if mf=ONCLICK then
      begin
        tmp:=ato_findempty();
        ato[tmp].p_new(tmp,now,ato_typ2sta(now),mx,my);
      end;
    plate.moveato();
  until false;

  closegraph;
end.
